package com.ohos.carbon.slice;

import com.ohos.carbon.LogUtils;
import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.animations.ImageFadeSlice;
import com.ohos.carbon.slice.animations.PathAnimationSlice;
import com.ohos.carbon.slice.animations.TouchRippleSlice;
import com.ohos.carbon.slice.animations.WidgetAnimationsSlice;
import com.ohos.carbon.slice.componentList.*;
import com.ohos.carbon.slice.demos.*;
import com.ohos.carbon.slice.dialog.*;
import com.ohos.carbon.slice.guidelines.*;
import com.ohos.carbon.slice.libraries.ConstraintLayoutSlice;
import com.ohos.carbon.slice.libraries.PicassoSlice;
import com.ohos.carbon.slice.themes.CurrentThemeSlice;
import com.ohos.carbon.slice.themes.DarkAndLightSlice;
import com.ohos.carbon.slice.themes.WindowsThemesSlice;
import com.ohos.carbon.slice.widgets.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MainAbilitySlice extends AbilitySlice {
    public static DatabaseHelper databaseHelper;
    public static Preferences preferences;
    public static Preferences pf;
    private Text often_text, often_;
    private Image image1, image2, what_image, favour_image, compoents_image, widgets_image, charts_image, gui_image;
    private AbilitySlice context;
    private DirectionalLayout what_lin, favour_lin, compoents_lin, widgets_lin, charts_lin, gui_lin, demo_lin, features_lin,
            libraries_lin, themes_lin, dialogs_lin, animations_lin, dynamic, oftenLayout;
    public static Map<String, Integer> allPreferences;


    public static int i = 0;
    public static HashMap<String, Integer> map;
    private HashMap<String, Integer> tempMap;
    private String key1 = "";
    private String key2 = "";

    public static Map<String, Integer> all = new HashMap<>();
    private Component dynamic_;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        databaseHelper = new DatabaseHelper(getContext()); // context入参类型为ohos.app.Context。
        String fileName = "favorites";
        preferences = databaseHelper.getPreferences(fileName);

        map = new HashMap<String, Integer>();
        tempMap = new HashMap<String, Integer>();
        pf = databaseHelper.getPreferences("name");
        all = (Map<String, Integer>) pf.getAll();
        initView();
    }

    private void initView() {
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        often_text = (Text) findComponentById(ResourceTable.Id_often_text);
        often_ = (Text) findComponentById(ResourceTable.Id_often_);

        favour_image = (Image) findComponentById(ResourceTable.Id_Favour_image);
        favour_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Favour_lin);
        what_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_what_lin);
        what_image = (Image) findComponentById(ResourceTable.Id_what_image);
        compoents_image = (Image) findComponentById(ResourceTable.Id_Components_image);
        compoents_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Components_lin);
        dynamic = (DirectionalLayout) findComponentById(ResourceTable.Id_dynamic);
        dynamic_ = findComponentById(ResourceTable.Id_dynamic_);
        oftenLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_often_layout);
        widgets_image = (Image) findComponentById(ResourceTable.Id_widgets_image);
        widgets_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Widgets_lin);
        charts_image = (Image) findComponentById(ResourceTable.Id_Charts_image);
        charts_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Charts_lin);
        gui_image = (Image) findComponentById(ResourceTable.Id_Gui_image);
        gui_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Gui_Lin);
        demo_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Demo_lin);
        features_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Features_lin);
        libraries_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Libraries_lin);
        themes_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Themes_lin);
        dialogs_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Dialogs_lin);
        animations_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_Animations_lin);
        gui_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_black_24dp));
        charts_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_show_chart_black_24dp));
        widgets_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_widgets_black_24dp));
        compoents_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_view_compact_black_24dp));
        favour_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_black_24dp));
        what_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_new_releases_black_24dp));
        image1.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_color_lens_black_24dp));
        image2.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_help_outline_black_24dp));
        image1.setClickedListener(component -> {
            Intent intent1 = new Intent();
//            present(new AboutSlice(), intent1);
//            present(new ColorsSlice(), intent1);
        });
        image2.setClickedListener(component -> {
            Intent intent2 = new Intent();
//            present(new ColorsSlice(), intent2);
            present(new AboutSlice(), intent2);
        });
        what_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new WhatsSlice(), intent);
        });
        favour_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new FavouritesSlice(), intent);
        });
        compoents_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new CompoentsSlice(), intent);
        });
        widgets_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new WidgetsSlice(), intent);
        });
        charts_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new ChartsSlice(), intent);
        });
        gui_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new GuiSlice(), intent);
        });
        demo_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new DemosSlice(), intent);
        });
        features_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new FeaturesSlice(), intent);
        });
        libraries_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new LibrariesSlice(), intent);
        });
        themes_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new ThemesSlice(), intent);
        });
        dialogs_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new DialogsSlice(), intent);
        });
        animations_lin.setClickedListener(component -> {
            Intent intent = new Intent();
            present(new AnimationsSlice(), intent);
        });
    }

    @Override
    public void onActive() {
        super.onActive();
        LogUtils.d("MainAbilitySlice", "进入页面");
        Iterator iter = all.entrySet().iterator();
        int num = 0;
        String tempKey = null;
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String) entry.getKey();
            int val = (Integer) entry.getValue();
            if (val < 100) {
                if (tempMap.size() != 0 && tempKey != null) {
                    if (val < tempMap.get(tempKey)) {
                        tempKey = key;
                        tempMap.put(key, val);
                    }
                }
                if (tempKey == null) {
                    tempKey = key;
                }
                num = Math.max(val, num);
            }
        }
        for (Map.Entry<String, Integer> entry1 : all.entrySet()) {
            if (entry1.getValue() == num) {
                key1 = entry1.getKey();
                LogUtils.d("MainAbilitySlice", "多" + key1 + "=" + entry1.getValue());
            }
            if (entry1.getValue() < num) {
                key2 = entry1.getKey();
                LogUtils.d("MainAbilitySlice", "少" + key2 + "=" + entry1.getValue());
            }
        }
        if (num == 0) {
            dynamic.setVisibility(Component.HIDE);
            dynamic_.setVisibility(Component.HIDE);
        } else {
            dynamic.setVisibility(Component.VISIBLE);
            dynamic_.setVisibility(Component.VISIBLE);
            oftenLayout.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {

                }
            });
            often_text.setText(key1);
            often_.setText("The most recently used sample");
            LogUtils.d("MainAbilitySlice", "赋值" + key1 + "=" + num);
            often_text.setClickedListener(component -> {
                Intent intent = new Intent();
                if (often_text.getText().equals("Widget animations")) {
                    present(new WidgetAnimationsSlice(), intent);
                } else if (often_text.getText().equals("Image fade")) {
                    present(new ImageFadeSlice(), intent);
                } else if (often_text.getText().equals("Touch ripple")) {
                    present(new TouchRippleSlice(), intent);
                } else if (often_text.getText().equals("Path animation")) {
                    present(new PathAnimationSlice(), intent);
                } else if (often_text.getText().equals("Icon and text list item")) {
                    present(new IconAndTextSlice(), intent);
                } else if (often_text.getText().equals("Avatar and text list item")) {
                    present(new AvatarAndTextSlice(), intent);
                } else if (often_text.getText().equals("Image,text,subtext and date list item")) {
                    present(new ImageAndTextSlice(), intent);
                } else if (often_text.getText().equals("Avatar,text,rating,subtext and date list item")) {
                    present(new RatingAndTextSlice(), intent);
                } else if (often_text.getText().equals("Registration form")) {
                    present(new RegistrationFromSlice(), intent);
                } else if (often_text.getText().equals("ComponentView")) {
                    present(new ComponentViewSlice(), intent);
                } else if (often_text.getText().equals("Power Menu")) {
                    present(new PowerMenuSlice(), intent);
                } else if (often_text.getText().equals("Auto Complete")) {
                    present(new AutoCompleteSlice(), intent);
                } else if (often_text.getText().equals("Quick Return")) {
                    present(new QuickReturnSlice(), intent);
                } else if (often_text.getText().equals("Search Toolbar")) {
                    present(new SearchToolbarSlice(), intent);
                } else if (often_text.getText().equals("Share toolbar")) {
                    present(new ShareToolbarSlice(), intent);
                } else if (often_text.getText().equals("Profile")) {
                    present(new ProfileSlice(), intent);
                } else if (often_text.getText().equals("Music player")) {
                    present(new MusicplayerSlice(), intent);
                } else if (often_text.getText().equals("Collapsing toolbar")) {
                    present(new CollapsingtoolbarSlice(), intent);
                } else if (often_text.getText().equals("Banner")) {
                    present(new BannerSlice(), intent);
                } else if (often_text.getText().equals("CheckBoxes & RadioButtons")) {
                    present(new CheckBoxesSlice(), intent);
                } else if (often_text.getText().equals("Buttons")) {
                    present(new ButtonsSlice(), intent);
                } else if (often_text.getText().equals("Floating Action Button")) {
                    present(new FloatingSlice(), intent);
                } else if (often_text.getText().equals("Circular progress")) {
                    present(new CircularSlice(), intent);
                } else if (often_text.getText().equals("Menus")) {
                    present(new MenusSlice(), intent);
                } else if (often_text.getText().equals("Progress bars")) {
                    present(new ProgressSlice(), intent);
                } else if (often_text.getText().equals("Snackbar")) {
                    present(new SnackbarSlice(), intent);
                } else if (often_text.getText().equals("Text fields")) {
                    present(new Text_filedsSlice(), intent);
                } else if (often_text.getText().equals("Tabs")) {
                    present(new TabsSlice(), intent);
                } else if (often_text.getText().equals("RecyclerView")) {
                    present(new RecyclerViewSlice(), intent);
                } else if (often_text.getText().equals("ExpandableRecyclerView")) {
                    present(new ExpandableRecyclerViewSlice(), intent);
                } else if (often_text.getText().equals("ExpansionPanel")) {
                    present(new ExpansionPanelSlice(), intent);
                } else if (often_text.getText().equals("DropDown")) {
                    present(new DropDownSlice(), intent);
                } else if (often_text.getText().equals("NavigationView")) {
                    present(new NavigationViewSlice(), intent);
                } else if (often_text.getText().equals("SeekBar")) {
                    present(new SeekBarSlice(), intent);
                } else if (often_text.getText().equals("FlowLayout & Chips")) {
                    present(new FolwLayoutSlice(), intent);
                } else if (often_text.getText().equals("TableLayout")) {
                    present(new TableLayoutSlice(), intent);
                } else if (often_text.getText().equals("BottomNavigationView")) {
                    present(new BottomNavigationSlice(), intent);
                } else if (often_text.getText().equals("BottomSheet")) {
                    present(new BottomSheetSlice(), intent);
                } else if (often_text.getText().equals("Backdrop")) {
                    present(new BackdropSlice(), intent);
                } else if (often_text.getText().equals("Bar chart")) {
                    present(new HistogramViewDemo(), intent);
                } else if (often_text.getText().equals("Line chart")) {
                    present(new ChartViewDemo(), intent);
                } else if (often_text.getText().equals("Gesture detectot")) {
                    present(new GesturedetectorSlice(), intent);
                } else if (often_text.getText().equals("Auto size text")) {
                    present(new AutoSizeTextSlice(), intent);
                } else if (often_text.getText().equals("Behavior")) {
                    present(new BehaviorSlice(), intent);
                } else if (often_text.getText().equals("HTML")) {
                    present(new HtmlSlice(), intent);
                } else if (often_text.getText().equals("XML Font")) {
                    present(new XmlFontSlice(), intent);
                } else if (often_text.getText().equals("Shadow")) {
                    present(new ShadowSlice(), intent);
                } else if (often_text.getText().equals("Pagination")) {
                    present(new PaginationSlice(), intent);
                } else if (often_text.getText().equals("Text appearance")) {
                    present(new TextAppearanceSlice(), intent);
                } else if (often_text.getText().equals("SVG icons")) {
                    present(new SVGiconsSlice(), intent);
                } else if (often_text.getText().equals("Z order")) {
                    present(new ZOrderSlice(), intent);
                } else if (often_text.getText().equals("Rounded corners")) {
                    present(new RoundedSlice(), intent);
                } else if (often_text.getText().equals("Anchors")) {
                    present(new AnchorsSlice(), intent);
                } else if (often_text.getText().equals("Text marker")) {
                    present(new TextMarkerSlice(), intent);
                } else if (often_text.getText().equals("Button/Usage")) {
                    present(new ButtonsUsageActivity(), intent);
                } else if (often_text.getText().equals("Menus/Behavior")) {
                    present(new MenusBehaviorActivity(), intent);
                } else if (often_text.getText().equals("Studies/Shrine")) {
                    present(new ShrineActivity(), intent);
                } else if (often_text.getText().equals("Studies/Carane")) {
                    present(new CraneActivity(), intent);
                } else if (often_text.getText().equals("Studies/Basil")) {
                    present(new BasilActivity(), intent);
                } else if (often_text.getText().equals("Picasso")) {
                    present(new PicassoSlice(), intent);
                } else if (often_text.getText().equals("ConstraintLayout")) {
                    present(new ConstraintLayoutSlice(), intent);
                } else if (often_text.getText().equals("Dark and Light")) {
                    present(new DarkAndLightSlice(), intent);
                } else if (often_text.getText().equals("Current Theme")) {
                    present(new CurrentThemeSlice(), intent);
                } else if (often_text.getText().equals("Windows Theme")) {
                    present(new WindowsThemesSlice(), intent);
                } else if (often_text.getText().equals("List dialog")) {
                    present(new ListDialogActivity(), intent);
                } else if (often_text.getText().equals("Progress dialog")) {
                    present(new ProgressDialogActivity(), intent);
                } else if (often_text.getText().equals("Select dialog")) {
                    present(new SelectDialogActivity(), intent);
                } else if (often_text.getText().equals("Simple dialog")) {
                    present(new SimpleDialogActivity(), intent);
                } else if (often_text.getText().equals("Dialog fragment")) {
                    present(new DialogFragmentActivity(), intent);
                }
            });
        }
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
